﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MyHomeworkWebAPi.Migrations
{
    /// <inheritdoc />
    public partial class AddOrderStatusCheckConstraint : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "categories",
                columns: table => new
                {
                    category_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "分类ID"),
                    name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "分类名称"),
                    is_use = table.Column<byte>(type: "tinyint", nullable: false, comment: "是否使用")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProductCategories", x => x.category_id);
                });

            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "用户ID"),
                    student_id = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false, comment: "学号"),
                    nickname = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "用户名"),
                    password = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false, comment: "密码"),
                    phone = table.Column<string>(type: "nvarchar(15)", maxLength: 15, nullable: false, comment: "手机号"),
                    avatar = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "头像")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_users", x => x.user_id);
                });

            migrationBuilder.CreateTable(
                name: "messages",
                columns: table => new
                {
                    message_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    content = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    is_read = table.Column<byte>(type: "tinyint", nullable: false),
                    create_time = table.Column<DateTime>(type: "datetime", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_messages", x => x.message_id);
                    table.ForeignKey(
                        name: "fk_messages_user_id",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "operation_log",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "日志ID"),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: true, comment: "用户ID"),
                    detail = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false, comment: "日志详情"),
                    create_time = table.Column<DateTime>(type: "datetime", nullable: false, defaultValueSql: "(getdate())", comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_operation_log", x => x.id);
                    table.ForeignKey(
                        name: "FK_Logs_Users",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateTable(
                name: "products",
                columns: table => new
                {
                    product_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "商品ID"),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "发布者ID"),
                    title = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false, comment: "商品标题"),
                    price = table.Column<decimal>(type: "decimal(10,2)", nullable: false, comment: "价格"),
                    category_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "分类ID"),
                    status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "状态"),
                    description = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false, comment: "描述"),
                    image = table.Column<byte[]>(type: "varbinary(max)", nullable: true, comment: "商品图片")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK__Products__47027DF5FA6ED102", x => x.product_id);
                    table.ForeignKey(
                        name: "FK_Products_Category",
                        column: x => x.category_id,
                        principalTable: "categories",
                        principalColumn: "category_id");
                    table.ForeignKey(
                        name: "FK__Products__user_i__3A81B327",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id");
                });

            migrationBuilder.CreateTable(
                name: "comments",
                columns: table => new
                {
                    comment_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "评论ID"),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "用户ID"),
                    product_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "商品ID"),
                    content = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false, comment: "评论"),
                    create_time = table.Column<DateTime>(type: "datetime", nullable: false, defaultValueSql: "(getdate())", comment: "创建时间"),
                    parentComment_id = table.Column<Guid>(type: "uniqueidentifier", nullable: true, comment: "父评论ID")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK__comments__E79576877142FE10", x => x.comment_id);
                    table.ForeignKey(
                        name: "FK_Comments_ParentComment",
                        column: x => x.parentComment_id,
                        principalTable: "comments",
                        principalColumn: "comment_id");
                    table.ForeignKey(
                        name: "FK_Comments_Products",
                        column: x => x.product_id,
                        principalTable: "products",
                        principalColumn: "product_id");
                    table.ForeignKey(
                        name: "FK_comments_users_user_id",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "interaction",
                columns: table => new
                {
                    interaction_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "互动行为id"),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "用户id"),
                    product_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "商品id"),
                    type = table.Column<int>(type: "int", nullable: false, comment: "类型（1：收藏；2：浏览）"),
                    create_time = table.Column<DateTime>(type: "datetime", nullable: false, comment: "时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_interaction", x => x.interaction_id);
                    table.ForeignKey(
                        name: "FK_interaction_products",
                        column: x => x.product_id,
                        principalTable: "products",
                        principalColumn: "product_id");
                    table.ForeignKey(
                        name: "FK_interaction_users",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "orders",
                columns: table => new
                {
                    order_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "订单ID"),
                    user_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "买家ID"),
                    product_id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, comment: "商品ID"),
                    status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "状态"),
                    create_time = table.Column<DateTime>(type: "datetime", nullable: false, defaultValueSql: "(getdate())", comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK__orders__4659622986B47BF5", x => x.order_id);
                    table.ForeignKey(
                        name: "FK__orders__product___45F365D3",
                        column: x => x.product_id,
                        principalTable: "products",
                        principalColumn: "product_id");
                    table.ForeignKey(
                        name: "FK__orders__user_id__44FF419A",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "user_id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_comments_parentComment_id",
                table: "comments",
                column: "parentComment_id");

            migrationBuilder.CreateIndex(
                name: "IX_comments_product_id",
                table: "comments",
                column: "product_id");

            migrationBuilder.CreateIndex(
                name: "IX_comments_user_id",
                table: "comments",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_interaction_product_id",
                table: "interaction",
                column: "product_id");

            migrationBuilder.CreateIndex(
                name: "IX_interaction_user_id",
                table: "interaction",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "idx_messages_user_id",
                table: "messages",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_operation_log_user_id",
                table: "operation_log",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_orders_product_id",
                table: "orders",
                column: "product_id");

            migrationBuilder.CreateIndex(
                name: "IX_orders_user_id",
                table: "orders",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_products_category_id",
                table: "products",
                column: "category_id");

            migrationBuilder.CreateIndex(
                name: "IX_products_user_id",
                table: "products",
                column: "user_id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "comments");

            migrationBuilder.DropTable(
                name: "interaction");

            migrationBuilder.DropTable(
                name: "messages");

            migrationBuilder.DropTable(
                name: "operation_log");

            migrationBuilder.DropTable(
                name: "orders");

            migrationBuilder.DropTable(
                name: "products");

            migrationBuilder.DropTable(
                name: "categories");

            migrationBuilder.DropTable(
                name: "users");
        }
    }
}
